package com.baozun.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baozun.mapper.TWorkrecordMapper;
import com.baozun.model.TWorkrecord;
import com.baozun.model.TWorkrecordExample;
import com.baozun.service.WorkrecordService;
import com.baozun.util.PageData;

@Service("workrecordService")
@Transactional
public class WorkrecordServiceImpl implements WorkrecordService {
    
    @Autowired
    private TWorkrecordMapper workrecordMapper;
    
    @Override
    public void add(TWorkrecord bean) {
        // TODO Auto-generated method stub
        if (StringUtils.isEmpty(bean.getStWorkrecordId())) {
            bean.setStWorkrecordId(UUID.randomUUID().toString());
        }
        bean.setDtCreateTime(new Date());
        workrecordMapper.insert(bean);
    }
    
    @Override
    public TWorkrecord get(String id) {
        // TODO Auto-generated method stub
        return workrecordMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public void update(TWorkrecord bean) {
        bean.setDtUpdateTime(new Date());
        workrecordMapper.updateByPrimaryKeyWithBLOBs(bean);
    }
    
    @Override
    public void delete(String stWorkrecordId) {
        // TODO Auto-generated method stub
        workrecordMapper.deleteByPrimaryKey(stWorkrecordId);
    }
    
    @Override
    public PageData<TWorkrecord> getPageListByExample(TWorkrecordExample record, int pageSize, int currentPage) {
        record.setSize(pageSize);
        record.setStart(pageSize * (currentPage - 1));
        PageData<TWorkrecord> infoList = new PageData<TWorkrecord>();
        List<TWorkrecord> appendixList = workrecordMapper.selectByExampleWithBLOBs(record);
        int count = workrecordMapper.countByExample(record);
        infoList.addAll(appendixList);
        infoList.setPage(currentPage);
        infoList.setPages((count + pageSize - 1) / pageSize);
        infoList.setPageSize(pageSize);
        infoList.setTotalRecords(count);
        return infoList;
    }
    
    @Override
    public List<TWorkrecord> getList(TWorkrecordExample record) {
        // TODO Auto-generated method stub
        record.getOrderByClause();
        return workrecordMapper.selectByExampleWithBLOBs(record);
    }

    @Override
    public int getCount(TWorkrecordExample record) {
        // TODO Auto-generated method stub
        return workrecordMapper.countByExample(record);
    }
    
    
}
